package com.vincent.kdp.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.rollviewpager.Util;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.CounterTotalViewHolder;
import com.vincent.kdp.adapter.CounterViewHolder;
import com.vincent.kdp.bean.CounterTotalOutput;
import com.vincent.kdp.bean.RxBusPosBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.repository.CheckGoodsRepository;
import com.vincent.kdp.ui.bean.response.Counter;
import com.vincent.kdp.ui.bean.response.CounterOutput;
import com.vincent.kdp.util.RxBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CounterListActivity extends AppCompatActivity implements RecyclerArrayAdapter.OnMoreListener{

    @Bind(R.id.toolbar_center_tv)
    TextView toolbarCenterTv;
    @Bind(R.id.counter_lv)
    EasyRecyclerView counterLv;
    private CheckGoodsRepository checkGoodsRepository = new CheckGoodsRepository();

    private RecyclerArrayAdapter<CounterTotalOutput.CounterTotal> adapter;
    private int num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counter_list);
        ButterKnife.bind(this);

        toolbarCenterTv.setText("选择柜台");
       // bean = (CounterOutput) getIntent().getExtras().getSerializable("bean");


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        counterLv.setLayoutManager(layoutManager);
        DividerDecoration itemDecoration = new DividerDecoration(Color.parseColor("#CDCDCD"), Util.dip2px(this, 0.2f), Util.dip2px(this, 10), Util.dip2px(this, 10));
        counterLv.addItemDecoration(itemDecoration);
        counterLv.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<CounterTotalOutput.CounterTotal>(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                CounterTotalViewHolder pvh;
                pvh = new CounterTotalViewHolder(parent);
                return pvh;
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                Intent it = new Intent(CounterListActivity.this, CounterGoodsListActivity.class);
                it.putExtra("id", adapter.getAllData().get(position).getCounterId());
                it.putExtra("name", adapter.getAllData().get(position).getCounterName());
                startActivity(it);

            }
        });
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        num=1;
        adapter.clear();
        getData(num);
    }

    private void getData(int num) {

        checkGoodsRepository.getCounterListTotal(num,new RequestCallBack<CounterTotalOutput>() {
            @Override
            public void onSuccess(CounterTotalOutput bean) {
                adapter.addAll(bean.getList());
                adapter.notifyDataSetChanged();
                if (bean.getPaginator().isLastPage()) {
                    if (bean.getPaginator().getPages() == 1) {
                        adapter.pauseMore();
                    } else {
                        adapter.stopMore();
                    }
                }

            }

            @Override
            public void onFalse(String msg) {
                // dialog(msg);
            }
        });

    }

    @OnClick(R.id.back_img)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onMoreShow() {
        num++;
        getData(num);

    }

    @Override
    public void onMoreClick() {

    }
}
